package cn.tedu.mintmall.controller;

import cn.tedu.mintmall.dto.SpuAddNewDTO;
import cn.tedu.mintmall.dto.SpuUpdateDTO;
import cn.tedu.mintmall.restful.JsonResult;
import cn.tedu.mintmall.service.ISpuService;
import cn.tedu.mintmall.vo.SpuStandardVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/spu")
@Api(tags = "SPU管理模块")
public class SpuController {

    @Autowired
    private ISpuService spuService;

    @PostMapping("/addNew")
    @ApiOperation("新增spu")
    public JsonResult<Void> addNew(@Validated SpuAddNewDTO spuAddNewDTO){
        spuService.addNew(spuAddNewDTO);
        return JsonResult.ok("新增完成");
    }

    @PostMapping("/delete")
    @ApiOperation("删除spu")
    @ApiImplicitParam(value = "id",name = "spuId",required = true,dataType = "long")
    public JsonResult<Void> deleteSpuById(Long id){
        spuService.deleteSpuById(id);
        return JsonResult.ok("删除成功");
    }

    @PostMapping("/{id}/update")
    @ApiOperation("根据id修改spu")
    @ApiImplicitParam(value = "id",name = "spuId",required = true,dataType = "long")
    public JsonResult<Void> updateSpuById(@PathVariable("id") Long id ,@Validated SpuUpdateDTO spuUpdateDTO){
        spuService.updateSpuById(id,spuUpdateDTO);
        return JsonResult.ok("修改完成");
    }

    @GetMapping("/select/{id}")
    @ApiOperation("查询spu详情")
    @ApiImplicitParam(value = "id",name = "spuId",required = true,dataType = "long")
    public JsonResult<SpuStandardVO> getSpuDetailById(@PathVariable("id") Long id){
        SpuStandardVO spuStandardVO = spuService.getSpuDetailById(id);
        return JsonResult.ok("查询详情完成",spuStandardVO);
    }
}
